//
//  HMApplyReasonTableViewCell.m
//  HMMarket
//
//  Created by haohao on 2017/6/26.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMApplyReasonTableViewCell.h"

@interface HMApplyReasonTableViewCell ()<UITextViewDelegate>
NS_STRONG UILabel *placeLabel;
NS_STRONG UILabel *numberOfLabel;
@end

@implementation HMApplyReasonTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];

    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addSubview:self.reasonTextView];
        
        _placeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 150, 29) ];
        _placeLabel.textColor = kc9c9c9;
        _placeLabel.font = [UIFont systemFontOfSize:15.0];
        _placeLabel.text = @"请输入退款原因";
        [_reasonTextView addSubview:_placeLabel];
        
        
        _numberOfLabel = [[UILabel alloc] initWithFrame:CGRectMake(_reasonTextView.frame.size.width - 100, 2, 100, 29) ];
        _numberOfLabel.textColor = kc9c9c9;
        _numberOfLabel.textAlignment = NSTextAlignmentRight;
        _numberOfLabel.font = [UIFont systemFontOfSize:15.0];
        _numberOfLabel.text = @"(200字)";
        [_reasonTextView addSubview:_numberOfLabel];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _reasonTextView.delegate = self;
    }
    return self;
}


#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (textView.text.length >= 200 && range.location == 200) {
        [HMHUDManager showHUDWithText:@"请少于200字"];
        return NO;
    } else {
        return YES;
    }
}


- (void)textViewDidChange:(UITextView *)textView {
    MyLog(@"textView.text.length = %ld", textView.text.length);
   
        if (textView.text.length > 0) {
            _placeLabel.hidden = YES;
            _numberOfLabel.hidden = YES;
        } else {
            _placeLabel.hidden = NO;
            _numberOfLabel.hidden = NO;
        }
        
        float height = [self.reasonTextView sizeThatFits:CGSizeMake(self.reasonTextView.frame.size.width, FLT_MAX)].height ;
        
        if ([_delegate respondsToSelector:@selector(returnRowHeight:textView:)]) {
            if (height + 15 > 49) {
                [_delegate returnRowHeight:height + 15 textView:textView.text];
                CGRect reasonFrame = self.reasonTextView.frame;
                if (height != 0) {
                    [UIView animateWithDuration:0.5 animations:^{
                        self.reasonTextView.frame = CGRectMake(reasonFrame.origin.x, reasonFrame.origin.y, reasonFrame.size.width, height);
                    }];
                }
            } else {
                [_delegate returnRowHeight:49 textView:textView.text];
                CGRect reasonFrame = self.reasonTextView.frame;
                if (height != 0) {
                    [UIView animateWithDuration:0.5 animations:^{
                        self.reasonTextView.frame = CGRectMake(reasonFrame.origin.x, reasonFrame.origin.y, reasonFrame.size.width, 39);
                    }];
                }
            }
        }
    

}

- (void)setText:(NSString *)text {
    _text = text;
    if (text.length != 0) {
        _reasonTextView.text = text;
    }
}

- (UITextView *)reasonTextView {
    if (_reasonTextView == nil) {
        _reasonTextView = [[UITextView alloc] initWithFrame:CGRectMake(20, 7.5, KWidthSCREEN - 40, 34)];
        _reasonTextView.font = [UIFont systemFontOfSize:15.0];
        _reasonTextView.scrollEnabled = NO;
    }
    return _reasonTextView;
}


- (CGRect)returnRectWithString:(NSString *)string width:(float)width{
    MyLog(@"width = %f", width);
    CGRect nameRect = [string boundingRectWithSize:CGSizeMake(width, 0)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} context:nil];
    return nameRect;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
